package com.baoer.service.impl;

import com.baoer.R;
import com.baoer.entity.Shift;
import com.baoer.entity.Trip;
import com.baoer.entity.Triptype;
import com.baoer.dao.TripDao;
import com.baoer.service.TripService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class TripSerrviceImpl implements TripService {

    @Autowired
    private TripDao tripMapper;
    @Value("${image.base.path}")
    private String imagePath;

    @Override
    public Map getTripList(Map<String,Object>map) {
        int pageIndex = Integer.parseInt(map.get("pageIndex").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());

        pageIndex = (pageIndex - 1) * pageSize;
        map.put("pageIndex", pageIndex);
        double countTrip = tripMapper.countTrip(map);
        int pagenum = (int)Math.ceil(countTrip / pageSize);
        List<Trip> TripList = tripMapper.getTripList(map);
        Map<String, Object> map1 = new HashMap<>();
//        List<Trip> tripList = TripList.stream()
//                .peek(trip -> {
//                    if (trip.getPicture() != null && !trip.getPicture().isEmpty()) {
//                        trip.setPicture(imagePath + trip.getPicture());
//                    }
//                })
//                .collect(Collectors.toList());
        map1.put("pagenum", pagenum);
        map1.put("triplist", TripList);
        return map1;
    }




    //添加旅行信息
    @Override
    public R addtrip(Trip trip) {
        return R.ok(tripMapper.addtrip(trip));
    }
    //修改旅行信息
    @Override
    public R updatetrip(Trip trip) {
        return R.ok(tripMapper.updatetrip(trip));
    }
    //删除旅行信息
    @Override
    public R deltrip(Integer tripId) {
        return R.ok(tripMapper.deltrip(tripId));
    }

    @Override
    public Trip tripinfo(Integer tripId) {
        return tripMapper.tripinfo(tripId);
    }

    @Override
    public List shiftlist(Integer tripId) {
        return tripMapper.shiftlist(tripId);
    }

    @Override
    public int addshift(Shift shift) {
        Integer tripId = shift.getTripId();
        Integer tripday = tripMapper.tripday(tripId);
        shift.setTripday(tripday);
        return tripMapper.addshift(shift);
    }

    @Override
    public Map triptypelist(Map<String, Object> map) {
        int pageIndex = Integer.parseInt(map.get("pageIndex").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        pageIndex = (pageIndex - 1) * pageSize;
        map.put("pageIndex", pageIndex);
        double counttriptype = tripMapper.countTriptype(map);
        int pagenum = (int)Math.ceil(counttriptype / pageSize);
        List triptypelist = tripMapper.triptypelist(map);
        Map<String, Object> map1 = new HashMap<>();
        map1.put("pagenum", pagenum);
        map1.put("triptypelist", triptypelist);
        return map1;
    }

    @Override
    public int addtriptype(Triptype triptype) {
        return tripMapper.addtriptype(triptype);
    }


}
